package portal.mybatis.dao.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;
import portal.mybatis.dao.Company;
import portal.mybatis.dao.mapper.CompanyMapper;
import portal.mybatis.page.Page;

@Service
public class CompanyService extends BaseService{
	@Autowired
	CompanyMapper companyMapper;
	public boolean insert(String name,String roleId){
		if(name==null ||name.length()==0)
			return false;
		Company comp =new Company(name,roleId);
		return companyMapper.insertSelective(comp)>0;
	}
	public boolean update(String name,String roleId,String id){
		return update(name,roleId,Integer.valueOf(id));
	}
	public boolean update(String name,String roleId,Integer id){
		if(id==null)return false;
		Company comp=new Company(name,roleId,id);
		return companyMapper.updateById(comp)>0;
	}
	public JSONObject selectOne(String Id,String name,String roleId){
		List<Object> l=selectByX(Id,name,roleId);
		if(l==null||l.size()==0)return null;
		return JSONObject.fromObject(l.get(0));
	}
	public List<Object> selectByX(String Id,String name,String roleId){
		try{
			return this.selectByX(Integer.valueOf(Id), name, roleId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public List<Object> selectByX(Integer Id,String name, String roleId){
		Company comp=new Company(name,roleId,Id);
		return convert2OjbectList(companyMapper.selectByX(comp));
	}
	public List<Object> selectByPage(Long offset,Long pageSize){
		Page<Company> page =new Page<>(pageSize,offset);
		return convert2OjbectList(companyMapper.selectByPage(page));
	}
	public Long countByPage(Long offset,Long pageSize){
		Page<Company> page =new Page<>(pageSize,offset);
		return companyMapper.countByPage(page);
	}
	public boolean deleteById(Long Id){
		if(Id==null)return false;
		return companyMapper.deleteById(Id)>0;
	}

}
